<?php
//测试控制器类
class IndexController extends CommonController{
	
	private $model; 
	
	public function __init(){
		parent::__init();
		$this->model = K('Goods');
	}
	
    //动作方法
    public function index(){
    	//显示轮播图
    	$this->assign('turnimg',K('ImgConfig')->order('sort DESC')->limit(7)->all());			
    	//显示新品上市  
    	$newGoods = $this->model->getNewGoods();
//		var_dump($newGoods);
		$this->assign('newgoods',$newGoods);
        //显示视图
        $this->display();
    }
	
	/**
	 * ajax获得商品名
	 */
	 public function getGoodsName(){
	 	if(!IS_AJAX) $this->error('非法请求');
		$cname = Q('get.goodsname');
		//搜索到商品
		$search_key = K('Goods')->field('goods_name,shop_price,goods_unit')->where("goods_name LIKE '%{$cname}%'")->limit(5)->all();
		
//		//返回当前分类下面的商品的数量
//		foreach($search_key as $key=>$val){
//			$search_key[$key]['sons'] = implode(',',K('Category')->getAllChild($val['cid']));
//			//将cid组为一个'1','12'
//			$search_key[$key]['sons'] = "'".str_replace(',',"','", $search_key[$key]['sons'])."'";
//			$search_key[$key]['goodsNum'] = $this->model->where("goods_cateid in ({$search_key[$key]['sons']})")->count();
//			unset($search_key[$key]['sons']);
//		}
		$this->ajax($search_key);
	 }
	
	
}
